﻿// IPC Library
// Interprocess Communication Library
// Copyright(C)2008 Bill Tudor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace IPCLibrary
{
	/// <summary>
	/// Mailslot Client.
	/// </summary>
	/// <remarks>
	/// The mailslot client class provides simple methods for sending both text messages
	/// and byte data messages to a mailslot.
	/// </remarks>
	/// <seealso cref="IPCLibrary.Mailslot"/>
	/// <seealso cref="IPCLibrary.MailslotBase"/>
	public class MailslotClient : MailslotBase
	{
		/// <summary>
		/// Creates a mailslot client.
		/// </summary>
		/// <param name="mailslotName">Mailslot name.</param>
		/// <remarks>
		/// The scope of this mailslot will be the local machine, i.e., ".".
		/// </remarks>
		public MailslotClient(string mailslotName)
			: base(mailslotName) { }
		/// <summary>
		/// Creates a mailslot client.
		/// </summary>
		/// <param name="mailslotScope">Mailslot scope.</param>
		/// <param name="mailslotName">Mailslot name.</param>
		/// <remarks>
		/// The mailslot scope can be a hostname, a domain name, or one of two
		/// special characters: use "*" for the current system's primary domain, or
		/// use "." to represent the current computer.
		/// <para></para>
		/// <para><para>For IPC between processes on the same machine, use ".".</para>
		/// For IPC between machines in the current domain, use "*".</para>
		/// <para>For IPC between the current host and a specific mailslot server, use the hostname
		/// of the remote server.</para>
		/// <para>For IPC between the current host and any number of servers on any computer in the
		/// host's domain, use either the "*" special name or the domain name".</para>
		/// </remarks>
		public MailslotClient(string mailslotScope, string mailslotName)
			: base(mailslotScope, mailslotName) { }
		/// <summary>
		/// Send data to the mailslot.
		/// </summary>
		/// <param name="data">Data to send.</param>
		/// <returns>Number of bytes sent.</returns>
		public int Send(byte[] data)
		{
			OpenClient();
			return WriteBytes(data, data.Length);
		}
		/// <summary>
		/// Send a message to the mailslot.
		/// </summary>
		/// <param name="message">Message to send.</param>
		/// <returns>Number of bytes sent.</returns>
		public int SendMessage(string message)
		{
			return SendMessage(message, Encoding.ASCII);
		}
		/// <summary>
		/// Send a message to the mailslot.
		/// </summary>
		/// <param name="message">Message to send.</param>
		/// <param name="characterEncoding">Character encoding to use.</param>
		/// <returns>Number of bytes sent.</returns>
		public int SendMessage(string message, Encoding characterEncoding)
		{
			return Send(characterEncoding.GetBytes(message));
		}
	}
}
